package org.firefly.tool.file;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class ReadWriteFile {

	/**
	 * 读取文件 使用NIO方式
	 * 
	 * @param filePath
	 * @return
	 */
	public static List<String> readTxtFileNIO(String filePath) {
		List<String> contentIds = new ArrayList<>();
		BufferedReader bufferedReader = null;
		try {
			Path path = Paths.get(filePath);
			// 判断文件是否存在
			bufferedReader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				contentIds.add(lineTxt.trim());
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出 ,e:{}" + e);
		} finally {
			closeIO(bufferedReader);
		}
		return contentIds;
	}

	/**
	 * 读取文件 BufferedReader
	 * 
	 * @param filePath
	 * @return
	 */
	public static List<String> readTxtFileBuffer(String filePath) {
		List<String> contentIds = new ArrayList<>();
		BufferedReader bufferedReader = null;
		InputStreamReader read = null;
		try {
			File file = new File(filePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在
				bufferedReader = new BufferedReader(new FileReader(file));
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					contentIds.add(lineTxt.trim());
				}
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出 ,e:{}" + e);
		} finally {
			closeIO(bufferedReader, read);
		}
		return contentIds;
	}

	/**
	 * 创建文件
	 *
	 * @param file
	 * @return
	 */
	public static boolean createFile(File file) throws Exception {
		try {
			return file.exists() || file.createNewFile();
		} catch (Exception e) {
			System.err.println("create file error e:{}" + e);
		}
		return false;
	}

	/**
	 * 追加内容到文件[普通方式]
	 * 
	 * @param content
	 * @param file
	 * @return
	 */
	public static boolean writeTxtFile(String content, File file) {
		boolean flag = false;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;

		try {
			content = content + "\n";
			fos = new FileOutputStream(file, true);
			bos = new BufferedOutputStream(fos);
			bos.write(content.getBytes("UTF-8"));
			bos.flush();
			flag = true;
		} catch (Exception e) {
			System.err.println("write file error e:{}" + e);
		} finally {
			closeIO(bos, fos);
		}
		return flag;
	}

	/**
	 * 追加内容到文件[NIO]
	 * 
	 * @param content
	 * @param file
	 * @return
	 */
	public static boolean writeTxtFileNIO(String content, String filePath) {
		boolean flag = false;
		BufferedWriter writer = null;
		try {
			content = content + "\n";
			Path src = Paths.get(filePath);
			if (!Files.exists(src)) {
				Files.createFile(src);
			}
			writer = Files.newBufferedWriter(src, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // 追加
			writer.write(content);
			flag = true;
		} catch (Exception e) {
			System.err.println("write file error e:{}" + e);
		} finally {
			closeIO(writer);
		}
		return flag;
	}

	public static void closeIO(Closeable... io) {
		for (Closeable i : io) {
			if (i != null) {
				try {
					i.close();
				} catch (IOException e) {
					System.err.println("io流关闭出错");
				}
			}
		}
	}
}
